const Com = require("../../gmoon/baseCom");
const STATE = require("../../states/gunstates"); 

cc.Class({
    extends: Com,

    properties: {
    },

    ctor(){
        this._gun = null;
        this._eid = $T.timestamp() + $T.random(1,100);
    },

    setSize( w , h){
        return $T.setNodeSize(this.node ,[w,h]);
    },

    isSame( elscr ){
        return elscr ? this._eid === elscr._eid : false;
    },

    _setGun( gun ){
        this._gun = gun;
    },

    getGun(){
        return this._gun;
    },

    setGetGunState( flag ){
        flag = $T.selectValue( flag , true );
        if( this.getAttr("_getGunState") === flag ) return;
        let globGetGunNode = $G.get('gloabGetGunStateNode');

        if(flag){
            cc.tween(this.node)
            .to(.1 ,{scale : 1.1})
            .start();
            globGetGunNode.setPosition(0,0);
            globGetGunNode.parent = this.node;
            globGetGunNode.active = true;
            $T.getScr(globGetGunNode).startAnimate();
            this.setAttr("_getGunState" , true );
        }else{
            cc.tween(this.node)
            .to(.1 ,{scale : 1})
            .start();
            $T.getScr(globGetGunNode).stopAnimate();
            globGetGunNode.active = false;
            this.setAttr("_getGunState" , false );
        }
    },

    _setFitGunParent( gun ){
        const panel = $G.get("controlpanel").childNode("panel");
        let pos = gun.getPosition();
        if(gun.parent){
            pos = gun.parent.convertToWorldSpaceAR( gun.getPosition() );
        }
        gun.parent = this.getAttr("_gunStateV") ===  STATE.INBIGHEBING ? cc.find("Canvas") : panel;
        gun.setPosition( gun.parent.convertToNodeSpaceAR(pos));
    },

    fitGun( gun ){
        const panel = $G.get("controlpanel").childNode("panel");
        let thispos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
        const state = this.getAttr("_gunStateV");
        const gunScr = $T.getScr(gun);
        const hebingState =  this.checkHebing(gunScr);
        $T.setNodeSize( gun , [,160]);

        if( hebingState=='hebing' ){

            //$HOOK.appendUpdate({
            $HOOK.append( "update" , {
                handler : this.playGunHebing.bind(this),
                params : gunScr,
                // check : ()=>{ return true } ,// 用户检查是否销毁
                //destory : function, //销毁逻辑 
                once : true //是否只执行一次,
                //time : 3 //定时执行多少秒
            });
            //this.playGunHebing( gunScr );

        }else if(hebingState == "move"){
            this._gun = $T.getScr(gun);
    
            const lastFather = this._gun.getAttr("_father");
            if( !this.isSame(lastFather) && lastFather ){
                lastFather._setGun( null );
            }
            //赋值底座信息
            this._gun.setAttr("_father" , this );
            this._gun.setState( state ).onStateChanged();
    
            this._setFitGunParent(gun);
    
            this.resetGunpos();
        }else{

        }
    },

    checkHebing( oriGun ){
        let re = "move" , oriId = oriGun.getAttr("_gid");
        if( this._gun && oriId != this._gun.getAttr("_gid") ){
            re = "hebing";
        }

        return re;
    },

    //合并指定gun到此处
    playGunHebing( dt , oriGun ){
        let srGun = this.getGun();
        if( !srGun || srGun.getAttr("_gid") == oriGun.getAttr("_gid")) return;
        srGun.setState( STATE.HEBING );
        oriGun.setState( STATE.HEBING );

        this._setFitGunParent( oriGun.node );

        const parent = srGun.node.parent;
        let thispos = parent.convertToNodeSpaceAR(this.node.parent.convertToWorldSpaceAR(this.node.getPosition())),
            leftpos = $T.getAnglePos( thispos , 180 , this.node.width/2 *1.1),
            rightpos = $T.getAnglePos( thispos , 0 ,  this.node.width/2 *1.1);
            
        cc.tween( srGun.node )
        .to(.2 ,{x:leftpos.x , y: leftpos.y})
        .to(.1 , { x : thispos.x , y: thispos.y} )
        .start();

        cc.tween( oriGun.node)
        .to(.2 ,{x:rightpos.x , y: rightpos.y})
        .to(.1 , { x : thispos.x , y: thispos.y} )
        .call(()=>{
            this._gun = null;
            this.fitGun( oriGun.node );
            $G.do('poolPutByNode' , "put" , srGun.node , 'gun');
        })
        .start();
    }
});
